<template>
  <DatePicker
    v-if="type === 'config-mobile' || type === 'mobile'"
    v-model="input"
    :label="config.title"
    :required="config.required"
  />
  <div v-if="type === 'config-table' || type === 'table'" class="v-form-item-wrapper">
    <div class="form-item">
      <el-date-picker
        v-model="input"
        :type="pickerType"
        :value-format="formatStr"
        :format="formatStr"
        :placeholder="config.placeholder"
        @change="onChange"
      />
      <div v-if="config.required" class="required">*</div>
    </div>
    <pre v-if="config.tips"><i class="icon iconfont">&#xe77e;</i>{{ config.tips }}</pre>
  </div>
</template>

<script setup lang="ts">
import DatePicker from '../../components/vant/DatePicker.vue'
const emits = defineEmits(['update'])
const props = defineProps<{
  id: string
  tdId: string
  formType: string
  formTypeName: string
  config: any

  type: string // form配置编辑 table表格表单 mobile移动端表单
  formValue: string
}>()
const pickerType = computed(() => {
  if (props.config.dateType === 0) {
    return 'date'
  }
  if (props.config.dateType === 1) {
    return 'datetime'
  }
  if (props.config.dateType === 2) {
    return 'year'
  }
  if (props.config.dateType === 3) {
    return 'month'
  }
})
const formatStr = computed(() => {
  if (props.config.dateType === 0) {
    return 'YYYY-MM-DD'
  }
  if (props.config.dateType === 1) {
    return 'YYYY-MM-DD HH:mm'
  }
  if (props.config.dateType === 2) {
    return 'YYYY'
  }
  if (props.config.dateType === 3) {
    return 'YYYY-MM'
  }
})
const input = ref<string>('')
const onChange = () => {
  if (input.value) {
    emits('update', input.value)
  }
}
onMounted(() => {
  input.value = props.formValue
})
</script>
